using COC.Debug;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using COC.Service;
using COC.Tool;
namespace COC.CEvent
{
	   public class CSelection:UnityEngine.Object
		{

				/// <summary>
				///  type active service
				/// </summary>
				private List<CTuple<CEventType,int,IService>> selector = new List<CTuple<CEventType,int,IService>> ();
				public readonly static int INACTIVE = 0;
				public readonly static int ACTIVE = 1;
				private static CSelection singleton;

				public static CSelection Instance ()
				{
						if (singleton == null) {
								singleton = new CSelection ();
						}
						return singleton;
				}
				/// <summary>
				/// Regist the specified component and type.
				/// </summary>
				/// <param name="component">Component.</param>
				/// <param name="type">Type.</param>
				public void Regist (IService component, CEventType type)
				{
						selector.Add (new CTuple<CEventType,int,IService> (type, ACTIVE, component));
				}
				/// <summary>
				/// Resign the specified component. 
				/// </summary>
				/// <param name="component">Component.</param>
				public CTuple<CEventType,int,IService> Resign (IService service)
				{
				
						foreach (CTuple<CEventType,int,IService>  c in selector) {
								IService s = c.Item3;
								if (s.serviceName.Equals (service.serviceName) && s.id == service.id) {
					                 //stop server and remove it.
					                  s.Stop();         
					                  selector.Remove(c);
                                      DebugTool.Log("removing service:" + s.serviceName + " is successful.");
					                  return c;
								}
						}
						return null;
				}
				/// <summary>
				/// enable to active component or not.
				/// </summary>
				/// <param name="o">O.</param>
				/// <param name="isActive">If set to <c>true</c> is active.</param>
				private void SetActive (IService o, bool isActive)
				{
						int size = selector.Count;
						if (size > 0) {
								foreach (CTuple<CEventType,int,IService> c in selector) {
										//if(c.Item3){

										//}
										if (o.serviceName.Equals (c.Item3.serviceName)) {

												c.Item2 = (isActive == true ? ACTIVE : INACTIVE);
										}
								}
						}
				}

				public List<CTuple<CEventType,int,IService>> GetSelector ()
				{
						return selector;
				}


		}

} 